Passed
Push — develop ( c592f3...b91919 )
by Endre
04:18
created

Action.loadPageConfig   A

Complexity

Conditions 2

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 21
dl 0
loc 23
ccs 7
cts 7
cp 1
crap 2
rs 9.376
c 0
b 0
f 0
1
import {IObserver, IObserverAdapter} from '../Observer/Observer';
2
import RouterRegistry from '../Router/Registry';
3
import Router, {IPageData} from '../Router/Router';
4
import {IAdapter, IModulePageData} from './Application';
5
import ModuleLoader from './ModuleLoader';
6
7
export default class Action {
8
  private menuOpenState: IObserver<boolean>;
9
  private router: Router;
10
  private routerAdapter: IObserverAdapter<IPageData | null>;
11
  private routerRegistry: RouterRegistry;
12
  private moduleLoader: ModuleLoader;
13
  private currentPage: IObserver<IPageData | null>;
14
15
  constructor(
16
    menuOpenState: IObserver<boolean>,
17
    currentPage: IObserver<IPageData | null>,
18
    router: Router,
19
    routerRegistry: RouterRegistry,
20
    routerAdapter: IObserverAdapter<IPageData | null>,
21
    moduleLoader: ModuleLoader
22
  ) {
23 8
    this.routerAdapter = routerAdapter;
24 8
    this.menuOpenState = menuOpenState;
25 8
    this.currentPage = currentPage;
26 8
    this.router = router;
27 8
    this.routerRegistry = routerRegistry;
28 8
    this.moduleLoader = moduleLoader;
29
  }
30
31
  get adapter(): IAdapter {
32 7
    return {
33
      onPageChanged: this.loadModule.bind(this),
34
      onGithubClick: this.openGithubWindow.bind(this),
35
      onMenuClick: this.switchMenuState.bind(this),
36
      onClose: this.closeMenu.bind(this),
37
      onMenu: this.switchPage.bind(this)
38
    };
39
  }
40
41
  public loadPageConfig() {
42 2
    const homePage: IModulePageData = {
43
      depth: 0,
44
      name: 'home',
45
      rootUrl: './',
46
      url: './',
47
      module: './HelloWorld'
48
    };
49 2
    const settingsPage: IModulePageData = {
50
      depth: 1,
51
      name: 'settings',
52
      rootUrl: './settings/',
53
      url: './settings/',
54
      module: './Settings/Settings'
55
    };
56 2
    this.routerRegistry.registerPage(homePage);
57 2
    this.routerRegistry.registerPage(settingsPage);
58
59 2
    if (this.currentPage.value == null) {
60 1
      this.router.changePage(homePage);
61
    } else {
62 1
      this.routerAdapter.onChange(this.currentPage.value);
63
    }
64
  }
65
66
  protected openGithubWindow(): void {
67 1
    window.open('https://github.com/enbock/Time-Tracker/', '_blank');
68
  }
69
70
  protected switchMenuState(): void {
71 1
    this.menuOpenState.value = !this.menuOpenState.value;
72
  }
73
74
  protected closeMenu(): void {
75 3
    this.menuOpenState.value = false;
76
  }
77
78
  protected switchPage(name: string): void {
79 1
    const page: IPageData = this.routerRegistry.getPages()[name];
80 1
    this.router.changePage(page);
81 1
    this.closeMenu();
82
  }
83
84
  protected async loadModule(newValue: IPageData): Promise<void> {
85 1
    await this.moduleLoader.loadModule((newValue as IModulePageData).module);
86
  }
87
}
88